package cn.tedu.person.controller;

import cn.tedu.person.pojo.dto.AdminInsertDTO;
import cn.tedu.person.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.person.pojo.dto.AdminUpdateDTO;
import cn.tedu.person.pojo.vo.AdminIndexVO;
import cn.tedu.person.pojo.vo.AdminStandardVO;
import cn.tedu.person.security.AdminDetails;
import cn.tedu.person.security.LoginPrincipal;
import cn.tedu.person.service.IAdminService;
import cn.tedu.person.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags ="02.工作人员" )
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation("添加管理员信息")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addNew")
    public JsonResult insert(@Valid AdminInsertDTO adminInsertDTO){
        System.out.println("DTO="+adminInsertDTO);
        adminService.addNew(adminInsertDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据Id删除信息")
    @PreAuthorize("hasAuthority('/mg/admin/delete')")
    @ApiOperationSupport(order = 300)
    @GetMapping("/{id:[0-9]+}/deleteById")
    public JsonResult delete(@Range(min=1,message = "删除失败，ID值无效！")
                                   @PathVariable Long id){
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据Id查询信息")
    @PreAuthorize("hasAuthority('/mg/admin/read')")
    @ApiOperationSupport(order = 300)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@Range(min=1,message = "查询失败，ID值无效！")
                             @PathVariable Long id){
        AdminStandardVO queryResult = adminService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @ApiOperation("启用管理员")
    @PreAuthorize("hasAuthority('/mg/admin/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="id",value="管理员ID",
            required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult setEnable(@PathVariable Long id){
        log.debug("开始处理【启用管理员】的请求，参数：{}",id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用管理员")
    @PreAuthorize("hasAuthority('/mg/admin/update')")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name="id",value="管理员ID",
            required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult setDisable(@PathVariable Long id){
        log.debug("开始处理【禁用管理员】的请求，参数：{}",id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    @ApiOperationSupport(order = 400)
    @ApiOperation("管理员登录")
    public JsonResult login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理登录的请求 参数：{}",adminLoginInfoDTO);
        String jwt=adminService.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("管理员信息列表")
    @PreAuthorize("hasAuthority('/mg/admin/read')")
    @ApiOperationSupport(order = 200)
    @GetMapping("")
    public JsonResult list(
            @ApiIgnore
            @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询管理员列表】的请求，参数：无");
        log.debug("当事人信息：{}",loginPrincipal);
        log.debug("当事人信息的ID：{}",loginPrincipal.getId());
        log.debug("当事人信息中的用户名：{}",loginPrincipal.getUsername());
        List<AdminIndexVO> list= adminService.list();
        return JsonResult.ok(list);
    }
}
